import { VantComponent } from '../common/component';
VantComponent({
  field: true,
  classes: ['field-class', 'input-class', 'cancel-class'],
  props: {
    // 1= 评价题：题目，200字以内，全部显示；评价选项：好、中、差、未评价
    // 2= 单选题：题目，200字以内，全部显示：选项：选项条数不限制，字数不限，全部显示
    // 3= 多选题：题目
    // 4= 图片上传
    // 5= 视频上传
    // 6= 打分题
    // 7= 描述题
    // 8= 小票题，填写单号、服务员、时间、客数、原单金额、实收金额，上传图片

    type: {
      type: Number,
      value: 0
    },
    title: {
      type: String,
      value: ""
    },
    titleInfo: {
      type: String,
      value: ""
    },
    selectState: {
      type: String,
      value: "1"
    },
    needindex: {
      type: Number,
      value: 0
    },
    selectVal: { // 选择
      type: Array,
      value: []
    },
    fileVal: { // 文件
      type: Array,
      value: []
    },
    numVal: { // 打分
      type: Number,
      value: null
    },
    textareVal: { // 描述
      type: String,
      value: ''
    },
    textgroup: { // 小票
      type: Object,
      value: {}
    },
    maxDate: {
      type: Number,
      value: null
    },
    minDate: {
      type: Number,
      value: null
    }
  },
  data: {
    videoInitTime: 1,
    ImgMaxSize: 1024 * 1024 * 1,
    validationStatus: false,
    type_1index: null,
    type_2index: null,
    type_3index: [],
    pickerShow: false,
    dateVal: '',
    currentDate: new Date().getTime(),
    selectValcopy: [],
    currentValue: 0,//打分题 最后值
    valcont: {
      type_1: '',
      type_7: '',
      type_8: [
        //小票题
        { title: "单号", modal: 'ticket_number', pInfo: "请输入单号", type: 'text', val: '' },
        { title: "服务员", modal: 'waiter', pInfo: "请输入服务员号", type: 'text', val: '' },
        { title: "客人数量", modal: 'diners', pInfo: "请输入客人数量", type: 'number', val: '' },
        { title: "用餐时间", modal: 'ticket_time', pInfo: "请输入用餐时间", type: 'date' },
        { title: "原单金额", modal: 'ticket_amount', pInfo: "请输入金额", type: 'number', val: '' },
        { title: "实收金额", modal: 'ticket_net_receipts', pInfo: "请输入金额", type: 'number', val: '' },
      ],
      type_8_modal: { ticket_number: '', waiter: '', diners: '', ticket_time: '', ticket_amount: '', ticket_net_receipts: '' },
      az: [
        { val: "A" },
        { val: "B" },
        { val: "C" },
        { val: "D" },
        { val: "E" },
        { val: "F" },
        { val: "G" },
        { val: "H" },
        { val: "I" },
        { val: "J" },
        { val: "K" },
        { val: "L" },
        { val: "M" },
        { val: "N" },
        { val: "O" },
        { val: "P" },
        { val: "Q" },
        { val: "R" },
        { val: "S" },
        { val: "T" },
        { val: "U" },
        { val: "V" },
        { val: "W" },
        { val: "X" },
        { val: "Y" },
        { val: "Z" },
      ]
    },
    fileList: [],
    selectCopy: [],
  },
  mounted() {
    this.setData({
      selectCopy: this.data.selectVal
    })
    let pages = getCurrentPages();
    let { options } = pages.pop();
    if (options.type == '2') { // 需要回显状态
      if (this.data.type == '1') {
        this.data.selectVal.length && this.data.selectVal.forEach((item, index) => {
          if (item.question_answer) {
            this.setData({
              type_1index: index
            })
          }
        })
      } else if (this.data.type == '2') {
        this.data.selectVal.length && this.data.selectVal.forEach((item, index) => {
          if (item.question_answer) {
            this.setData({
              type_2index: index
            })
          }
        })
      } else if (this.data.type == '3') {
        const arr = []
        this.data.selectVal.length && this.data.selectVal.forEach((item, index) => {
          if (item.question_answer) {
            const key = 'selectCopy[' + index + '].active'
            this.setData({
              [key]: '1'
            })
            // this.data.selectVal[index].active = '1'
            arr.push(index)
          }
        })
        this.setData({
          type_3index: arr
        })
      } else if (this.data.type == '4' || this.data.type == '5') {
        const pathList = []
        this.data.fileVal.length && this.data.fileVal.map(item => pathList.push({ path: item.path }))
        this.setData({
          fileList: pathList
        })
      } else if (this.data.type == '6') {
        this.setData({
          currentValue: this.data.numVal
        })
      } else if (this.data.type == '7') {
        this.setData({
          'valcont.type_7': this.data.textareVal
        })
      } else if (this.data.type == '8') {
        this.data.valcont.type_8.forEach((element, index) => {
          if (element.modal === 'ticket_time') {
            this.setData({
              dateVal: this.data.textgroup[element.modal]
            })
          } else {
            const key = 'valcont.type_8[' + index + '].val'
            this.setData({
              [key]: this.data.textgroup[element.modal]
            })
          }
        });
        const pathList = []
        this.data.fileVal.length && this.data.fileVal.map(item => pathList.push({ path: item.path }))
        this.setData({
          fileList: pathList
        })
      }
    }

  },
  methods: {
    pickerShowFun() { // 用餐时间
      this.setData({
        pickerShow: true
      })
    },
    onConfirm(event) { // 确定用餐时间
      const dateStr = this.formatDate(new Date(event.detail))
      this.setData({
        dateVal: dateStr
      })
      this.$emit("ticketChange", {
        modal: event.target.dataset.modal,
        text: dateStr,
        needindex: event.target.dataset.needindex,
        type: event.target.dataset.type
      })
      this.setData({ pickerShow: false });
    },
    formatDate(date) { // 时间数据转换
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      var minute = date.getMinutes();
      minute = minute < 10 ? ('0' + minute) : minute;
      var second = date.getSeconds();
      second = second < 10 ? ('0' + second) : second;
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    },
    onClose() { // 取消用餐时间
      this.setData({ pickerShow: false });
    },
    getType_8_inputval(event) { // 小票
      this.setData({
        telphone: event.detail.value
      });
      this.$emit("ticketChange", {
        modal: event.target.dataset.modal,
        text: event.detail.value,
        needindex: event.target.dataset.needindex,
        type: event.target.dataset.type
      })
    },
    fieldInput_type7(event) {
      const { type, needindex } = event.currentTarget.dataset;
      this.$emit("typeClick", {
        type: type,//题目类型
        needindex: needindex,//大项 下的第几个小题
        text: event.detail //
      })
    },
    onDrag(event) {
      this.setData({
        currentValue: event.detail,
      });
      this.$emit('dragNum', { num: event.detail, needindex: event.target.dataset.index })
    },
    type_click(event) {
      const { index, type, needindex } = event.currentTarget.dataset;
      if (type == "1") {
        this.setData({
          type_1index: index
        });
      } else if (type == "2") {
        this.setData({
          type_2index: index
        });
      } else { // 多选
        let arr = this.data.type_3index
        const findIndex = this.data.type_3index.findIndex(v => v === index)
        const key = 'selectCopy[' + index + '].active'
        if (findIndex !== -1) {
          arr.splice(findIndex, 1)
          this.setData({
            [key]: '0'
          })
        } else {
          arr.push(index)
          this.setData({
            [key]: '1'
          })
        }
        this.setData({
          type_3index: arr
        })
        // 多选回值
        this.$emit("typeClick", {
          type: type,//题目类型
          needindex: needindex,//大项 下的第几个小题
          indexarr: this.data.type_3index //选择小题的第几项
        })
        return false; //结束
      }
      // 单选回值
      this.$emit("typeClick", {
        type: type,//题目类型
        needindex: needindex,//大项 下的第几个小题
        index: index //选择小题的第几项
      })
    },
    oversize() {
      wx.showToast({
        title: '文件大小不能超过' + this.data.ImgMaxSize / 1024 / 1024 + 'M',
        icon: 'none',
      })
    },
    afterRead(event) { // 图片读取完
      const { file } = event.detail;
      const fileAll = file.concat(this.data.fileList)
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      const arr = fileAll.map(item => { // $emit path 返回数组['路径']格式
        return item.path
      })
      this.$emit('postImg', { path: arr, needindex: event.target.dataset.index, type: event.target.dataset.type });
      this.setData({
        fileList: fileAll
      })
    },
    deleteImg(event) { // 删除图片
      const { file, index } = event.detail;
      const arr = this.data.fileList
      arr.splice(index, 1);
      this.$emit('delImg', { path: file.path, needindex: event.target.dataset.index, type: event.target.dataset.type });
      this.setData({
        fileList: arr
      })
    },
    afterReadVideo(event) { // 视频读取完
      const { file } = event.detail;
      if (file.duration > 10) {
        wx.showToast({
          title: '视频时长不能超过10秒',
          icon: 'none',
        })
      } else {
        this.$emit('postVideo', { file: file, needindex: event.target.dataset.index });
        this.setData({
          fileList: [file]
        })
      }

    },
    deleteVideo() {
      this.$emit('delVideo', { file: this.data.fileList[0], needindex: this.data.needindex });
      this.setData({
        fileList: []
      })
    },
  }
});
